<template>
	<div class="sys-grantData-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="450px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ state.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" label-position="top">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('system.role.dataScope') + '：'">
						  <g-sys-dict v-model="state.ruleForm.dataScope" code="DataScopeEnum" render-as="select" :placeholder="$t('system.role.dataScope')" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" v-if="state.ruleForm.dataScope === 5">
						<el-form-item :label="$t('system.org.orgList') + '：'">
							<OrgTree ref="orgTreeRef" class="w100" :tenant-id="state.ruleForm.tenantId" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">{{ $t('system.btn.cancel') }}</el-button>
					<el-button type="primary" @click="submit">{{ $t('system.btn.confirm') }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysGrantData">
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import OrgTree from '/@/views/system/organization/component/orgTree.vue';

import { apiSysRoleOwnOrgListGet,apiSysRoleGrantDataScopePost } from '/@/api-services/api';

const emits = defineEmits(['handleQuery']);
const orgTreeRef = ref();
const { t } = useI18n();
const state = reactive({
	title: t('system.role.grantData') as String,
	isShowDialog: false,
	ruleForm: {} as any
});

// 打开弹窗
const openDialog = async (row: any) => {
	state.title = t('system.role.grantData') + "：[ " + row.name + " ]";
	state.ruleForm = JSON.parse(JSON.stringify(row));
	var res = await apiSysRoleOwnOrgListGet(row.id);
	setTimeout(() => {
		orgTreeRef.value?.setCheckedKeys(res.data.data);
	}, 100);
	state.isShowDialog = true;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 提交
const submit = async () => {
	if (state.ruleForm.dataScope === 5) state.ruleForm.orgIdList = orgTreeRef.value?.getCheckedKeys();
	await apiSysRoleGrantDataScopePost(state.ruleForm);
	closeDialog();
};

// 导出对象
defineExpose({ openDialog });
</script>
